package com.itheima.service.db;

import com.itheima.domain.mongo.Question;
import com.itheima.domain.mongo.Report;
import com.itheima.domain.mongo.Soul;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class SoulServiceImpl implements SoulService {

    @Autowired
    MongoTemplate mongoTemplate;

    //测灵魂   问卷列表
    @Override
    public List<Soul> querySoulList(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<Soul> soulList = mongoTemplate.find(query, Soul.class);

        return soulList;
    }

    @Override
    public List<Question> queryQuestionList(List<ObjectId> questionIds) {
        Query query = Query.query(Criteria.where("id").in(questionIds));
        List<Question> questionList = mongoTemplate.find(query, Question.class);
        return questionList;
    }

    @Override
    public String saveReport(Report report) {
        Query query = Query.query(Criteria.where("soulId").is(report.getSoulId()));
        Report one = mongoTemplate.findOne(query, Report.class);
        if (null == one) {
            Update update = new Update();
            update.set("conclusion", report.getConclusion());
            update.set("cover", report.getCover());
            update.set("dimensions", report.getDimensions());
            mongoTemplate.updateFirst(query, update, Report.class);

            return one.getId().toHexString();
        }
        report.setId(new ObjectId());
        mongoTemplate.save(report);
        //开放下套试题
        Query query1 = Query.query(Criteria.where("id").is(report.getSoulId()));
        Soul soul = mongoTemplate.findOne(query1, Soul.class);
        Update update = new Update();
        update.set("isLock", 0);
        Query query2 = null;
        if (soul.getStar() == 2) {
            query = Query.query(Criteria.where("userId").is(report.getUserId()).and("star").is(3));
            mongoTemplate.updateFirst(query, update, Soul.class);
        } else if (soul.getStar() == 3) {
            query = Query.query(Criteria.where("userId").is(report.getUserId()).and("star").is(5));
            mongoTemplate.updateFirst(query, update, Soul.class);
        }

        return report.getId().toHexString();
    }

    //查询结果报告
    @Override
    public Report queryReport(String reportId) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(reportId)));
        return mongoTemplate.findOne(query, Report.class);
    }

    //根据测试题的id去查结果id
    @Override
    public String queryReportId(String soulId) {
        Query query = Query.query(Criteria.where("soulId").is(new ObjectId(soulId)));
        Report report = mongoTemplate.findOne(query, Report.class);
        if (null == report) {
            return null;
        }
        return report.getId().toHexString();
    }

    //根据题目id  查询题目
    @Override
    public Question queryQuestion(String questionId) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(questionId)));
        return mongoTemplate.findOne(query, Question.class);
    }

    //通过题目  level 和userId拿到对应的问卷id
    @Override
    public String querySoulId(String level, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("level").is(level));
        Soul soul = mongoTemplate.findOne(query, Soul.class);
        if (null == soul) {
            return null;
        }
        return soul.getId().toHexString();
    }

    //查询与你类型相同的人
    @Override
    public List<Report> querySimilarYou(Report report) {
        Soul soul = mongoTemplate.findOne(Query.query(Criteria.where("id").is(report.getSoulId())), Soul.class);

        List<Report> reportList = mongoTemplate.find(new Query(), Report.class);
        ArrayList<ObjectId> soulIds = new ArrayList<>();
        for (Report report1 : reportList) {
            soulIds.add(report1.getSoulId());
        }
        Query query = Query.query(Criteria.where("id").in(soulIds).and("star").is(soul.getStar()));
        List<Soul> soulList = mongoTemplate.find(query, Soul.class);
        soulIds.clear();
        for (Soul soul1 : soulList) {
            soulIds.add(soul1.getId());
        }
        Query query1 = Query.query(Criteria.where("conclusion").is(report.getConclusion()).and("soulId").in(soulIds));
        return mongoTemplate.find(query1, Report.class);
    }

    //根据type获取题目
    @Override
    public List<Question> queryQuestions(Integer type) {
        Query query = Query.query(Criteria.where("type").is(type));
        return mongoTemplate.find(query, Question.class);
    }

    //保存
    @Override
    public String saveSoul(Soul soul) {
        mongoTemplate.save(soul);
        return soul.getId().toHexString();
    }
}
